/**
 * 用户详情对话框
 */
var UserInfoDlg = {
    data: {
        deptId: "1111",
        deptName: ""
    }
};

layui.use(['layer', 'form', 'admin', 'laydate', 'ax', 'formSelects','element'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var layer = layui.layer;
    var formSelects = layui.formSelects;
    var element = layui.element;


    form.on('submit(btnSubmit1)', function (data) {
        // alert(JSON.stringify(data.field));
        var ajax = new $ax(Feng.ctxPath + "/share/test1" ,function (data) {
            if (data.code == 200){
                Feng.success("操作成功！");
                // layer.close(this);
                admin.closeThisDialog();
            }
        });
        ajax.set(data.field);
        ajax.start();
        return false;
    });

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/share/copySchedule" ,function (data) {

            //400错误，合成有冲突，选择需要的行程
            if (data.code ==400){
                $('#userForm').empty();
                // alert(data.message+'!!!'+JSON.stringify(data.data));

                var htmlopen = '<div class="layui-fluid" style="padding-bottom: 100px;">';
                htmlopen += '<div class="layui-card"><div class="layui-card-header">以下行程存在冲突，请选择要留下的日程</div>';
                htmlopen += '   <div class="layui-card-body">';
                htmlopen += '<input name="scheduleId" type="hidden" value="'+data.data.scheduleId+'"/>';
                var num1 =0;
                for (var ele in data.data.scheduleitem){
                    htmlopen += '<div class="layui-card"><div class="layui-card-header">' + ele + '</div>';
                    htmlopen += '   <div class="layui-card-body">';
                    for (var value in data.data.scheduleitem[ele]){
                        htmlopen += '<input class="abcx" type="radio" name="scheduleitemId['+num1+']" lay-skin="primary" value="'+data.data.scheduleitem[ele][value].scheduleitemId+'" title="'+data.data.scheduleitem[ele][value].startTime+'-'+data.data.scheduleitem[ele][value].endTime +'&emsp;'+data.data.scheduleitem[ele][value].scheduleitemName+'"/><br>';
                    }
                    num1+=1;
                    htmlopen += '   </div>';
                    htmlopen += '</div>';


                    // htmlopen +=''
                    // htmlopen += '<div id="output_detail" class="pace-done" style="padding: 40px;  line-height: 5px;" > <p style="word-wrap:break-word; word-break:break-all;">'+ ele + '</p> <br><br>';
                    // htmlopen += '<input type=checkbox name=details id=checkcheck>导出详情</div>';
                }
                htmlopen += '</div></div></div>';
                htmlopen += '<div class="form-group-bottom text-center">';
                htmlopen += '        <button class="layui-btn" lay-filter="btnSubmit1" lay-submit>&emsp;确定&emsp;</button>';
                htmlopen += '        <button type="reset" class="layui-btn layui-btn-primary" ew-event="closeDialog">&emsp;取消&emsp;</button>\n';
                htmlopen += '</div>';
                $('#userForm').append(htmlopen);
                form.render();
                // parent.layer.open({
                //     type: 1,
                //     title:'选择需要的日程项',
                //     area: ['90%', '95%'],
                //     fixed: false, //不固定
                //     maxmin: true,
                //     // content: '<div id="output_detail" class="pace-done" style="padding: 40px;  line-height: 5px;" > <p style="word-wrap:break-word; word-break:break-all;">'+ '确定导出执行计划吗？' + '</p> <br><br><input type=checkbox name=details id=checkcheck>导出详情</label></div>',
                //     content: htmlopen,
                //     closeBtn : 1,
                //     btn : [ '确认','取消' ],
                //     yes:function(index, layero){
                //         // location.href="${ctx}/datavalid/planResult/export?id="+resultId+"&check="+$("#checkcheck").attr("checked");
                //         /*   $.ajax({
                //               method : "get",
                //               url : "${ctx}/datavalid/planResult/export?id="+resultId+"&check="+$("#checkcheck").attr("checked"),
                //               success : function(data) {
                //                     alert(data);
                //
                //                      }
                //                   });   */
                //         var abc = $(".abcx").attr("checked");
                //         alert(abc);
                //         parent.layer.closeAll();
                //         // layer.close(index);
                //     }
                // });

            } else {
                Feng.success("合成成功！");
                admin.closeThisDialog();
            }
            // alert(JSON.stringify(data))

        }, function (data) {
            Feng.error("合成失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();
        //刷新父页面
        // parent.location.reload();
        // 添加 return false 可成功跳转页面
        // layer.close(loadIndex);
        return false;
    });



    /**
     * 初始化日程框架
     */
    var otherOrMyself='';

    /**
     * 初始化日程
     * @param data
     */
    loadSchedule();
    function loadSchedule(){
        $.ajax({
            url:Feng.ctxPath + "/share/getOtherSchedule",
            success:function (message) {
                $('#scheduleName').append('<option value="">请选择日程</option>');
                $.each(message.data, function(index, item) {
                    // alert(index+"\t===="+item)
                    $('#scheduleName').append('<option value="'+item.scheduleId+'">'+item.scheduleName+'</option>');
                });
                //重新渲染表单
                form.render('select');
                if (Feng.getUrlParam("scheduleId")!=null){
                    form.val("userForm",{"scheduleId":Feng.getUrlParam("scheduleId")});
                    showFrame(Feng.getUrlParam("scheduleId"));
                }
            }
        })
    }

    form.on('select(getFrame)', function(data) {
        $('#scheduleMessage').empty();
        if (data.value!='') {
            showFrame(data.value)
        }
    });


    /**
     * 显示一周样式
     */
    function showFrame(data){
        $.ajax({
            url: Feng.ctxPath + "/share/share_getScheduleAndFrameitem",
            data: {"scheduleId": data},
            success: function (message) {
                form.val("userForm",{"startData":message.data.startData,"endData":message.data.endData});

                schedulemessage = '';
                schedulemessage += '<div class="layui-inline layui-col-md6">';
                schedulemessage += '    <label class="layui-form-label">开始时间</label>';
                schedulemessage += '    <div class="layui-input-block">';
                schedulemessage += '        <input  type="text" class="layui-input date-icon datetime" autocomplete="off" disabled value="'+message.data.startData+'" />';
                schedulemessage += '    </div>';
                schedulemessage += '</div>';
                schedulemessage += '<div class="layui-inline layui-col-md6">';
                schedulemessage += '    <label class="layui-form-label">结束时间</label>';
                schedulemessage += '    <div class="layui-input-block">';
                schedulemessage += '        <input  type="text" class="layui-input date-icon datetime" autocomplete="off" disabled value="'+message.data.endData+'" />';
                schedulemessage += '    </div>';
                schedulemessage += '</div>';
                schedulemessage += '<div class="layui-inline layui-col-md12">';
                schedulemessage += '    <label class="layui-form-label">描述</label>';
                schedulemessage += '    <div class="layui-input-block">';
                schedulemessage += '        <input placeholder="请输入内容" class="layui-input" value="'+message.data.description+'" disabled/>';
                schedulemessage += '    </div>';
                schedulemessage += '</div>';

                schedulemessage += '<table class="layui-table" >';
                schedulemessage += '   <colgroup><col width="150"><col width="100"><col width="100"><col width="100"><col width="100"><col width="100"><col width="100"><col width="100"></colgroup>';
                schedulemessage += '   <thead><tr ><th></th><th>星期一</th><th>星期二</th><th>星期三</th><th>星期四</th><th>星期五</th><th>星期六</th><th>星期日</th></tr></thead>';

                $.each(message.data.frameitems, function (index, item) {
                    schedulemessage += '<tr><td >时间：' + item.startTime + '-' + item.endTime + '</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>';
                });
                $("#scheduleMessage").append(schedulemessage);
                form.render();
            }
        })
    }


    timeAdd();
    //初始化时间框
    function timeAdd(){
        lay('.datetime').each(function() {
            //时间选择器
            laydate.render({
                elem: this
                ,trigger: 'click'
                ,type: 'date'
            });
        });
    }

    //初始化我的日程
    formSelects.config('selMySchedule', {
        searchUrl: Feng.ctxPath + "/schedule/schedule_list",
        keyName: 'scheduleName',
        keyVal: 'scheduleId'
    });
    //初始化其他的日程
    formSelects.config('selOtherSchedule', {
        searchUrl: Feng.ctxPath + "/share/getShareSchedule?otherOrmyself=other",
        keyName: 'scheduleName',
        keyVal: 'scheduleId'
    });

});